import {
	USER_LOGIN,
	NEW_USER,
	INIT_LOCATION,
	MY_TARGET
} from "./requestUrl.js"
import {
	request
} from './requst.js'
import store from '../store/index.js'

export function newUser(data) {
	return request({
		url: NEW_USER,
		method: 'post',
		data
	})
}

export function userLogin(data) {
	return request({
		url: USER_LOGIN,
		method: 'post',
		data
	})
}

// 上传定位信息
export function setInitLocation(data) {
	return request({
		url: INIT_LOCATION,
		method: 'post',
		data
	})
}

export function myTarget() {
	return request({
		url: '/index/Demo/ChangeStauts',
		method: 'post',
		data: {
			user_id: store.state.userInfo.user_id
		}
	}, false)
}

// 轮询监听组队结果
export function listenerTeam() {
	let timer = setInterval(async () => {
		// 函数
		const result = await myTarget();
		if (!result.flag) {
			return
		} else {
			console.log('新任务')
			uni.showToast({
				title: '你有新任务',
				icon: 'none'
			})
			clearInterval(timer)
			// 修改状态
			store.commit('setTimer', null)
			uni.setStorageSync('lostTarget', JSON.stringify(result))
			store.commit('setLocationStatus', 1)
			console.log('新任务', result)
		}
	}, 5000)
	store.commit('setTimer', timer)
}

export function getMyLostTarget() {
	return request({
		url: MY_TARGET,
		method: 'post',
		data: {
			user_id: store.state.userInfo.user_id
		}
	})
}
